package br.com.dlm.simuladoitil.fragments;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import java.util.List;

import br.com.dlm.simuladoitil.R;
import br.com.dlm.simuladoitil.activities.ListaQuestoesActivity;
import br.com.dlm.simuladoitil.basicas.Categoria;
import br.com.dlm.simuladoitil.basicas.Questao;
import br.com.dlm.simuladoitil.util.ListaQuestoesAdapter;

public class ListaQuestoesFragment extends GeralFragment {

    private Categoria categoria;
    private List<Questao> questoes;

    private ListView listViewQuestoes;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        categoria = (Categoria) getActivity().getIntent().getSerializableExtra(ListaQuestoesActivity.EXTRA_CATEGORIA);

        View view = inflater.inflate(R.layout.fragment_questoes, container, false);

        listViewQuestoes = (ListView) view.findViewById(R.id.listViewQuestoes);

        verificarQuestoesDaRodada();

        listViewQuestoes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (getActivity() instanceof OnClickQuestaoListener) {
                    OnClickQuestaoListener listener = (OnClickQuestaoListener) getActivity();
                    listener.clickQuestao(categoria, position);
                }
            }
        });

        exibirQuestaoAutomatico();

        return view;
    }

    public void verificarQuestoesDaRodada() {
        if (questoes == null) {
            questoes = conteudo().obterQuestoesDaRodada(categoria);
        } else {
            questoes = conteudo().atualizarMudancasNasQuestoes(questoes);
        }

        categoria.questoes = questoes;

        carregarInformacoesNaTela();
    }

    public void verificarQuestoesDaNovaRodada() {
        categoria.questoes = conteudo().consultarQuestoesPorCategoria(categoria);
        questoes = conteudo().obterQuestoesDaRodada(categoria);
        categoria.questoes = questoes;

        carregarInformacoesNaTela();
    }

    private void carregarInformacoesNaTela() {
        getActivity().setTitle(categoria.nome);
        listViewQuestoes.setAdapter(new ListaQuestoesAdapter(questoes));
    }

    private void exibirQuestaoAutomatico() {
        if (getResources().getBoolean(R.bool.tablet)) {
            if (getActivity() instanceof OnClickQuestaoListener) {
                OnClickQuestaoListener listener = (OnClickQuestaoListener) getActivity();
                listener.clickQuestao(categoria, 0);
            }
        }
    }

    public interface OnClickQuestaoListener {
        void clickQuestao(Categoria categoria, int posicao);
    }
}
